package cn.onein.edu.web.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.onein.edu.biz.bean.LearnCategory;
import cn.onein.edu.biz.bean.LearnContent;
import cn.onein.edu.biz.bean.LearnIndex;
import cn.onein.edu.biz.bean.LearnSection;
import cn.onein.edu.biz.bean.LearnSectionDetail;
import cn.onein.edu.web.dao.LearnDao;
import cn.onein.edu.web.service.LearnService;
import cn.onein.edu.web.vo.LearnCategroyVO;
import cn.onein.edu.web.vo.LearnIndexGroupContentVO;

/**
 * 育儿百科首页 service接口实现
 * 
 * @author Justin
 * 
 */
@Service
public class LearnServiceImpl implements LearnService {

	@Autowired
	private LearnDao learnDao;

	/**
	 * 初始化育儿百科首页
	 */
	public Map<String, Object> getLearnIndex() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<LearnIndexGroupContentVO> vos = new ArrayList<LearnIndexGroupContentVO>();

		List<LearnSection> sections = learnDao.getAllSection();
		for (LearnSection o : sections) {
			// 得到二级目录
			String title = o.getTitle();
			title = title.replace("０～１岁", "零至一岁");
			title = title.replace("１～３岁", "一至三岁");
			title = title.replace("３～６岁", "三至六岁");
			List<LearnCategory> categorys = learnDao.getCategoryBySection(o
					.getId());
			map.put(title, categorys);

			// 得到最新文章
			String latest = title + "最新文章";
			List<LearnContent> laestContents = learnDao
					.getLatestContentBySection(o.getId());
			map.put(latest, laestContents);

			// 得到最热门文章前20名
			String hot = title + "最热门文章";
			List<LearnContent> hotContents = learnDao.getSectionHotTopContent(o.getName(),15);
			map.put(hot, hotContents);
			
			String detailTitle = title + "标题";
			LearnSectionDetail detail = learnDao.getSectionTitleBySectionName(o.getName());
			map.put(detailTitle, detail);
			
			String detailImage = title + "图片";
			List<LearnSectionDetail> images = learnDao.getSectionImageBySectionName(o.getName());
			map.put(detailImage, images);
			
			String detailText = title + "文本";
			List<LearnSectionDetail> texts = learnDao.getSectionTextBySectionName(o.getName());
			map.put(detailText, texts);
		}
		
		List<LearnIndex> images = learnDao.getLearnIndexImage();
		map.put("images", images);
		map.put("imageCount", images.size());
		
		List<LearnIndex> groups = learnDao.getLearnIndexGroup();
		for(LearnIndex group : groups){
			LearnIndexGroupContentVO vo = new LearnIndexGroupContentVO();
			List<LearnIndex> groupContents = learnDao.getLearnIndexContentByGroup(group.getGroupName());
			vo.setGroupName(group.getGroupName());
			vo.setContents(groupContents);
			vos.add(vo);
		}
		map.put("groups", vos);

		return map;
	}

	/**
	 * 一级分类最热门文章模块
	 */
	public Map<String, Object> getHotContentBySectionName(String sectionName,int count) {
		Map<String, Object> map = new HashMap<String, Object>();

		// 得到当前一级目录
		LearnSection section = learnDao.getSectionBySectionName(sectionName);

		if (null == section) {
			return null;
		}

		// 得到最热门文章通过一级目录名称
		List<LearnContent> hotContents = learnDao.getSectionHotTopContent(sectionName,count);

		map.put("hotContents", hotContents);
		map.put("section", section);
		return map;
	}

	/**
	 * 二级目录分类，按照一级目录
	 */
	public Map<String, Object> getContentGroupByCategory(
			Map<String, Object> map, String name) {
		List<LearnCategroyVO> list = new ArrayList<LearnCategroyVO>();

		// 得到当前一级目录
		LearnSection section = learnDao.getSectionBySectionName(name);
		if (null == section) {
			return null;
		}
		// 通过一级目录，得到二级目录
		List<LearnCategory> categorys = learnDao.getAllCategoryBySection(section.getId());
		for (LearnCategory o : categorys) {
			LearnCategroyVO vo = new LearnCategroyVO();
			vo.setImage(o.getImg());
			vo.setDescription(o.getDescription());
			// 得到二级目录对应的文章
			List<LearnContent> contents = learnDao.getContentGroupByCategory(o
					.getId());
			vo.setContents(contents);
			vo.setName(o.getName());
			vo.setTitle(o.getTitle());
			list.add(vo);
		}

		map.put("categoryList", list);
		return map;
	}

	/**
	 * 得到当前二级目录
	 */
	public LearnCategory getCurrCategoryByCategoryName(String name) {
		LearnCategory category = learnDao.getCategoryByCategoryName(name);

		return category;
	}

	/**
	 * 得到最热门文章，按照二级目录
	 */
	public void getHotContentByCategory(Map<String, Object> map, Long categoryId,String sectionName) {
		List<LearnContent> hotContents;
		
		// 得到该二级目录的最热门文章
		if(null == categoryId){
			hotContents = learnDao.getSectionHotTopContent(sectionName,12);
		}else{
			hotContents = learnDao.getHotContentByCategory(categoryId);
		}

		map.put("hotContents", hotContents);
	}
	
	/**
	 * 得到二级目录文章
	 */
	public void getPerpageContentByCategory(Map<String,Object> map,Long categoryId,String sectionName,Integer currPage,Integer perpageSize){
		Map<String,Object> paramMap = new HashMap<String,Object>();
		List<LearnContent> contents;
		
		Integer beginIndex = (currPage-1) * perpageSize;
		paramMap.put("perPage", perpageSize);
		paramMap.put("beginIndex", beginIndex);
		
		if(null == categoryId){
			paramMap.put("sectionName", sectionName);
			contents = learnDao.getALLContentBySectionName(paramMap);
		}else{
			paramMap.put("categoryId", categoryId);
			contents = learnDao.getPerpageContentByCategory(paramMap);
		}
		
		map.put("contents", contents);
	}

	//按照分类得到文章总数
	public Integer getContentCountsByCategory(Long categoryId,String sectionName){
		if(null == categoryId){
			return learnDao.getALLContentCountsBySectionName(sectionName);
		}else{
			return learnDao.getContentCountByCategory(categoryId);
		}
		
	}
	
	public void getSectionDetail(Map<String,Object> map,String sectionName){
		
		//得到一级目录文本信息
		List<LearnSectionDetail> textSections = learnDao.getSectionTextBySectionName(sectionName);
		
//		//得到一级目录标题
		LearnSectionDetail titleSection = learnDao.getSectionTitleBySectionName(sectionName);
		
		map.put("textSections", textSections);
		map.put("titleSection", titleSection);
	}
	
	public LearnSection getSectionBySectionName(String sectionName){
		return learnDao.getSectionBySectionName(sectionName);
	}

	@Override
	public void getLearnMenu(Map<String, Object> param) {
		List<LearnSection> sections = learnDao.getAllSection();
		for (LearnSection o : sections) {
			// 得到二级目录
			String title = o.getTitle();
			title = title.replace("０～１岁", "零至一岁");
			title = title.replace("１～３岁", "一至三岁");
			title = title.replace("３～６岁", "三至六岁");
			List<LearnCategory> categorys = learnDao.getCategoryBySection(o
					.getId());
			param.put(title, categorys);
		}
	}
	
}
